﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
namespace NewsWcfWebService
{
    public class _24hGetInfo
    {
        static string pageUrl = "http://hcm.24h.com.vn/";
        static htmlParse request = new htmlParse(pageUrl);
        public static List<ItemNewsInfo> getCatelogeMenu()
        {
            List<ItemNewsInfo> list = new List<ItemNewsInfo>();
            HtmlNode rootCateloge = request.HtmlDoc.DocumentNode.SelectSingleNode(".//div[@class='bam-xem-danh-muc']/ul[@class='nav']/li[@class='sub-li']/ul");
            HtmlNodeCollection listNodeCateloge = rootCateloge.ChildNodes;
            foreach (HtmlNode node in listNodeCateloge)
            {          
                if (node.FirstChild != null)
                {
                    ItemNewsInfo info = new ItemNewsInfo();
                   
                    HtmlNodeCollection listChildCateloges = node.ChildNodes;
                    if (listChildCateloges.Count > 1)
                    {
                        HtmlNode nodeA = listChildCateloges["a"];
                        if (nodeA != null)
                        {
                            HtmlAttributeCollection arrAttribute = nodeA.Attributes;
                            info.Tilte = arrAttribute["title"].Value.ToString();
                            info.Url = arrAttribute["href"].Value.ToString();
                        }
                        HtmlNode nodeUl = listChildCateloges["ul"];
                        if (nodeUl != null)
                        {
                            HtmlNodeCollection listChildNode = nodeUl.ChildNodes;
                            List<ItemNewsInfo> listCateloges = new List<ItemNewsInfo>();
                            if (listChildNode.Count > 0)
                            {
                                foreach (HtmlNode nodeChild in listChildNode)
                                {
                                    ItemNewsInfo info1 = new ItemNewsInfo();
                               
                                    if (nodeChild.FirstChild != null)
                                    {
                                        HtmlAttributeCollection arrAttribute = nodeChild.FirstChild.Attributes;
                                        info1.Tilte = arrAttribute["title"].Value.ToString();
                                        info1.Url = arrAttribute["href"].Value.ToString();
                                        listCateloges.Add(info1);
                                    }
                                }
                                info.ListCateloges = listCateloges;
                            }
                        }
                    }
                    else
                    {
                        HtmlAttributeCollection arrAttribute = node.FirstChild.Attributes;
                        info.Tilte = arrAttribute["title"].Value.ToString();
                        info.Url = arrAttribute["href"].Value.ToString();
    
                    }
                    list.Add(info);
                }
                
            }
            return list;
        }

        //static ItemNewsInfo convertNodeToInfo(HtmlNode node)
        //{
            
        //}
    }
}
